package com.hs.pre.module.sort;

import java.sql.SQLException;

import com.hs.HsBase;
import com.hs.core.db.HsPreparedStatement;
import com.hs.core.db.HsResultSet;
import com.hs.log.HsLogger;
import com.hs.pre.model.SortBean;

public class InitSortData {
	private static final HsLogger logger = HsLogger.getLogger(InitSortData.class.getName());
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		InitSortData init = new InitSortData();
		init.init(HsBase.getAnsiSqlMap().get("presort.init")+"");
		
	}
	/**
	    * 根据isRoot字段查询特殊记录
	    */
	   public int selectByIsRoot(String str){ 
		   int sortId = 0;
		   try {
				HsPreparedStatement st = new HsPreparedStatement("select sortid from app.pre_sort where ISROOT=?");
				st.setInt(1, Integer.parseInt(str));
				HsResultSet rs = st.executeQuery(); 
				
			    if(null != rs && rs.next()){
			    	sortId = rs.getInt("sortid");
			    }
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			} 
			return sortId;
	   }
	/**
	    * 删除分类数据
	    */
	   public void delSortData(String sql){ 
		   try {
				HsPreparedStatement st = new HsPreparedStatement(sql); 
				int count = st.executeUpdate();
				logger.severe("删除分类数据" + count + "条!");
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			} 
	   }
	   /**
	    * 初始化数据
	    * @param sql
	    */
	   private void initData(String sql){
		   try {
				HsPreparedStatement st = new HsPreparedStatement(sql);  
				int count = st.executeUpdate();
				logger.fine("初始化分类数据"+sql);
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			} 
	   }
	   /**
	    * 初始化分类数据
	    * @param sqls
	    */
	   public void init(String sqls){ 
		   String[] temp = sqls.split("@");
		   if(null != temp){
			   for(int i = 0;i<temp.length;i++){
				   initData(temp[i]);
			   }
		   }
	   }
	   /**
	    * 判断分类表中是否存在数据
	    * @param sortName
	    * @param sql
	    * @return
	    */
	   public boolean isExsit(String sql){
		   boolean  flag = false;
		   try {
				HsPreparedStatement st = new HsPreparedStatement(sql);  
				HsResultSet rs = st.executeQuery(); 
				if(null != rs && rs.next()){
					if(rs.getInt("count")>0)
						flag = true;
				}
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			} 
		   return flag;
	   }
	   /**
	    * 根据分类名称判断该分类是否存在
	    * @param sortName
	    * @param sql
	    * @return
	    */
	   public String isExsitByName(SortBean bean,String sql,String insertSql){ 
		   String  sortid = null;
		   try {
				HsPreparedStatement st = new HsPreparedStatement(sql);
				st.setString(1, bean.getSortName());
				HsResultSet rs = st.executeQuery(); 
				if(null != rs && rs.next()){
					sortid = rs.getInt("sortid") + ""; 
				}
				System.out.println("sortId" + sortid);
				if(null == sortid)
				{
					HsPreparedStatement stt = new HsPreparedStatement(insertSql);
					stt.setString(1, bean.getSortName());
					stt.setString(2, bean.getSortName());
					stt.setInt(3, bean.getParentSortId());
					stt.setInt(4, 1); 
					sortid = stt.executeUpdateReturnId();
				}
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			}  
			return sortid;
	   }
	   /**
	    * 新增分类并返回新增主键
	    * @param sortName
	    * @param sql
	    * @return
	    */
	   public String newSort(SortBean bean,String sql){ 
		   String  sortid = null;
		   try {
				HsPreparedStatement st = new HsPreparedStatement(sql);  
				st.setString(1, bean.getSortName());
				st.setString(2, bean.getSortRemark());
				st.setInt(3, bean.getParentSortId());
				//st.setInt(3, bean.getIsUse());
				st.setInt(4, bean.getIsRoot());
				//st.setString(5, bean.getImageUrl());
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			}  
			return sortid;
	   }
	   /**
	    * 新增分类与方剂关联关系
	    * @param sortName
	    * @param sql
	    * @return
	    */
	   public String newRelation(int sortId,int fangjiid,int type,String sql){ 
		   String  sortid = null;
		   try {
				HsPreparedStatement st = new HsPreparedStatement(sql);  
				st.setInt(1, fangjiid);
				st.setInt(2, sortId);
				st.setInt(3, type);  
				st.executeUpdate();
			} catch (SQLException e) { 
				e.printStackTrace();
			} catch (Exception e) { 
				e.printStackTrace();
			}  
			return sortid;
	   }
	   
	   
}
